﻿<Serializable()> _
Public Class Result

#Region "Members"
    Private m_resultID As Integer
    Private m_ResultAction As ResultAction
    Private m_ResultProperties As IresultProperties
#End Region

#Region "Properties"

    Public Property ResultID() As Integer
        Get
            Return m_resultID
        End Get
        Set(ByVal value As Integer)
            m_resultID = value
        End Set
    End Property

    Public Property ResultAction() As ResultAction
        Get
            Return m_ResultAction
        End Get
        Set(ByVal value As ResultAction)
            m_ResultAction = value
        End Set
    End Property

    Public Property ResultProperties() As IresultProperties
        Get
            Return m_ResultProperties
        End Get
        Set(ByVal value As IresultProperties)
            m_ResultProperties = value
        End Set
    End Property

    Public Property action() As ResultAction
        Get
            Return m_ResultAction
        End Get
        Set(ByVal value As ResultAction)
            m_ResultAction = value
        End Set
    End Property

    Public Property properties() As IresultProperties
        Get
            Return m_ResultProperties
        End Get
        Set(ByVal value As IresultProperties)
            m_ResultProperties = value
        End Set
    End Property

    Public Sub New(ByVal p_ResultID As Integer)
        m_resultID = p_ResultID
    End Sub
#End Region

#Region "XML Serialization"

#Region "Consts"
    Private Const TYPE As String = "type"
    Private Const ELEMENT_ID As String = "elementId"
    Private Const TEXT As String = "text"
    Private Const MOVIE_ID As String = "movieId"
    Private Const TIME As String = "time"
    Private Const IS_PAUSE As String = "isMoviePaused"
    Private Const Is_SMOOTH As String = "IsSmoothTransition"


    Private Const URL_FILE As String = "url"
    Private Const FILE_NAME As String = "fileName"
    Private Const FILE_SUFFIX As String = "fileSuffix"
    Private Const MEDIA_FILE_ID As String = "MediaFileID"

    Private Const DURATION As String = "duration"
    Private Const IS_FLICKERING As String = "isFlickering"
    Private Const IS_MOVIE_PAUSED As String = "isMoviePaused"

    Private Const TOP_LEFT As String = "locationTopLeft"
    Private Const BOTTOM_RIGHT As String = "locationBottomRight"
    Private Const NUM_OF_FRAMES As String = "numOfFrames"
    Private Const RESULT_ID As String = "ResultID"

#End Region

    Public Sub XmlDesirialize(ByVal p_xelement As XElement, ByVal p_mediaFiles As List(Of MediaFile))
        Me.ResultID = p_xelement.Attribute(RESULT_ID).Value
        Me.ResultAction = GetResultActionFromString(p_xelement.Attribute(TYPE).Value)
        Select Case ResultAction
            Case GameEditor.ResultAction.changeGlobalElementText
                Dim l_CGEProperties = New ChangeGlobalEventTextResultProperties
                l_CGEProperties.ElementID = p_xelement.Attribute(ELEMENT_ID).Value
                l_CGEProperties.Text = p_xelement.Attribute(TEXT).Value
                Me.ResultProperties = l_CGEProperties
            Case GameEditor.ResultAction.moveToMovie
                Dim l_MTMProperties As New MoveToMovieResultProperties
                l_MTMProperties.Movie = (From l_file In p_mediaFiles Where l_file.ID = p_xelement.Attribute(MOVIE_ID).Value Select l_file).SingleOrDefault
                l_MTMProperties.StartingAt = p_xelement.Attribute(TIME).Value

                Try
                    l_MTMProperties.IsSmoothTransition = p_xelement.Attribute(Is_SMOOTH).Value
                Catch ex As Exception

                End Try

                Me.ResultProperties = l_MTMProperties
            Case GameEditor.ResultAction.playAudio
                Dim l_PAProperties As New PlaySoundResultProperties
                l_PAProperties.AudioFileID = p_xelement.Attribute(MEDIA_FILE_ID).Value
                l_PAProperties.IsMoviePaused = CBool(p_xelement.Attribute(IS_MOVIE_PAUSED).Value)
                ResultProperties = l_PAProperties
            Case GameEditor.ResultAction.showPicture
                Dim l_SPProperties As New ShowPictureResultProperties
                l_SPProperties.IsMoviePaused = CBool(p_xelement.Attribute(IS_MOVIE_PAUSED).Value)
                l_SPProperties.IsFlickering = CBool(p_xelement.Attribute(IS_FLICKERING).Value)
                l_SPProperties.Duration = p_xelement.Attribute(DURATION).Value
                l_SPProperties.Location = GetLocationAttributesFromXML(p_xelement)
                l_SPProperties.Picture = (From l_file In p_mediaFiles Where l_file.ID = p_xelement.Attribute(MEDIA_FILE_ID).Value Select l_file).SingleOrDefault
                l_SPProperties.NumOfFrames = p_xelement.Attribute(NUM_OF_FRAMES).Value
                Me.ResultProperties = l_SPProperties

        End Select
    End Sub

    Private Function GetLocationAttributesFromXML(ByVal p_xelement As XElement) As EventRectangle
        Dim l_arrTopLeft = p_xelement.Attribute(TOP_LEFT).Value.Split(",")

        Dim l_TopLeft = New Point(l_arrTopLeft(0), l_arrTopLeft(1))

        Dim l_arrBottomRight = p_xelement.Attribute(BOTTOM_RIGHT).Value.Split(",")
        Dim l_BottomRight = New Point(l_arrBottomRight(0), l_arrBottomRight(1))

        Return New EventRectangle(l_TopLeft, l_BottomRight)
    End Function

    Public Function GetXMLString(ByVal p_MediaFiles As List(Of MediaFile)) As XElement
        Dim l_xElement As New XElement("Result")
        l_xElement.Add(New XAttribute(TYPE, ResultAction))
        l_xElement.Add(New XAttribute(RESULT_ID, ResultID))

        Select Case ResultAction
            Case GameEditor.ResultAction.changeGlobalElementText
                Dim l_CGETProperties = CType(Me.ResultProperties, ChangeGlobalEventTextResultProperties)
                l_xElement.Add(New XAttribute(ELEMENT_ID, l_CGETProperties.ElementID))
                l_xElement.Add(New XAttribute(TEXT, l_CGETProperties.Text))

            Case GameEditor.ResultAction.moveToMovie
                Dim l_MTMProperties = CType(Me.ResultProperties, MoveToMovieResultProperties)
                l_xElement.Add(New XAttribute(MOVIE_ID, l_MTMProperties.MovieID))
                l_xElement.Add(New XAttribute(TIME, l_MTMProperties.StartingAt))

                l_xElement.Add(New XAttribute(Is_SMOOTH, l_MTMProperties.IsSmoothTransition))

            Case GameEditor.ResultAction.playAudio
                Dim l_PAProperties = CType(Me.ResultProperties, PlaySoundResultProperties)
                l_xElement.Add(New XAttribute(IS_PAUSE, l_PAProperties.IsMoviePaused))
                InsertURLFileAttributes(l_xElement, p_MediaFiles, getURLFromID(l_PAProperties.AudioFileID, p_MediaFiles), l_PAProperties.AudioFileID)

            Case GameEditor.ResultAction.showPicture
                Dim l_SPProperties = CType(Me.ResultProperties, ShowPictureResultProperties)
                l_xElement.Add(New XAttribute(IS_MOVIE_PAUSED, l_SPProperties.IsMoviePaused))
                l_xElement.Add(New XAttribute(IS_FLICKERING, l_SPProperties.IsFlickering))
                l_xElement.Add(New XAttribute(DURATION, l_SPProperties.Duration))
                InsertURLFileAttributes(l_xElement, p_MediaFiles, l_SPProperties.Picture.Url, l_SPProperties.Picture.ID)
                l_xElement.Add(New XAttribute(TOP_LEFT, l_SPProperties.Location.TopLeft))
                l_xElement.Add(New XAttribute(BOTTOM_RIGHT, l_SPProperties.Location.BottomRight))
                l_xElement.Add(New XAttribute(NUM_OF_FRAMES, l_SPProperties.NumOfFrames))
            Case Else
        End Select

        Return l_xElement
    End Function

    Private Function getURLFromID(ByVal p_ID As Integer, ByVal p_mediaFiles As List(Of MediaFile)) As String
        Dim l_URL = (From l_Media In p_mediaFiles Where l_Media.ID = p_ID Select l_Media.Url).Single
        Return l_URL
    End Function

    Private Sub InsertURLFileAttributes(ByVal p_xElement As XElement, ByVal p_mediaFiles As List(Of MediaFile), ByVal p_Url As String, ByVal p_mediaFileID As String)
        Dim l_Url As String = p_Url
        Dim l_FileName As String = ""
        Dim l_FileSuffix As String = ""



        l_FileName = IO.Path.GetFileNameWithoutExtension(l_Url)
        l_FileSuffix = IO.Path.GetExtension(l_Url).Substring(1)


        p_xElement.Add(New XAttribute(URL_FILE, l_Url))
        p_xElement.Add(New XAttribute(FILE_NAME, l_FileName))
        p_xElement.Add(New XAttribute(FILE_SUFFIX, l_FileSuffix))
        p_xElement.Add(New XAttribute(MEDIA_FILE_ID, p_mediaFileID))
    End Sub

    Private Function GetResultActionFromString(ByVal p_typeString As String) As ResultAction
        If p_typeString.Equals(GameEditor.ResultAction.changeGlobalElementText.ToString) Then
            Return GameEditor.ResultAction.changeGlobalElementText
        ElseIf p_typeString.Equals(GameEditor.ResultAction.killApplication.ToString) Then
            Return GameEditor.ResultAction.killApplication
        ElseIf p_typeString.Equals(GameEditor.ResultAction.moveToMovie.ToString) Then
            Return GameEditor.ResultAction.moveToMovie
        ElseIf p_typeString.Equals(GameEditor.ResultAction.none.ToString) Then
            Return GameEditor.ResultAction.none
        ElseIf p_typeString.Equals(GameEditor.ResultAction.playAudio.ToString) Then
            Return GameEditor.ResultAction.playAudio
        ElseIf p_typeString.Equals(GameEditor.ResultAction.showPicture.ToString) Then
            Return GameEditor.ResultAction.showPicture
        End If
    End Function
#End Region

End Class
